Spring Boot 使用 git

您所在的位置:网站首页 git id Spring Boot 使用 git

Spring Boot 使用 git

2024-07-04 09:12| 来源: 网络整理| 查看: 265

原文地址:https://www.cnblogs.com/Naylor/p/18024689

简介

git-commit-id-maven-plugin 是一个maven 插件,用来在打包的时候将 git-commit 信息打进 Jar 中。

这样做的好处是可以将发布的某版本和对应的代码关联起来,方便查阅和线上项目的维护。至于它的作用,用官方说法,这个功能对于大型分布式项目来说是无价的。

功能

你是否经常遇到这样的问题:

测试提交了一个bug,开发人员无法确认是哪个版本有这个问题?当前测试环境部署的是某个版本吗?生产环境会不会也有这个问题? 公司内部的项目,总共几十、几百个服务,每天都有服务的生产环境部署,一个服务甚至一天上线好几次,对于项目管理来说无法清晰了解某一时刻某个服务的版本 如何验证我的代码是否已经上线? 。。。。。。

以上种种,都有一个共同的诉求,就是我希望在打包的时候将最后一次 git commit id 和当前 jar 关联起来并可试试查询 jar 对应的 git commit id 。

实践 引入插件

本例 Spring Boot 版本为 2.7.6,java 版本为 11

此插件已经上传到中央仓库(https://central.sonatype.com/artifact/io.github.git-commit-id/git-commit-id-maven-plugin?smo=true)

在项目pom.xml 中引入如下插件

    ......                                                     io.github.git-commit-id                 git-commit-id-maven-plugin                 5.0.0                                                             get-the-git-infos                                                     revision                                                 initialize                                                                         true                     ${project.build.outputDirectory}/git.                                             ^git.build.(time|version)$                         ^git.commit.id.(abbrev|full)$                                         txt                     full                                         generateGitPropertiesFilename:用于指定生成的 gitCommitInfo 存放到哪个位置,后缀可以任意指定,如果不指定将使用 format 的值 format:指定文件后缀,一般为 properties、json commitIdGenerationMode:记录完整信息,若 format 为 json,此值必须为 full

此外为了能成功打出 jar 包,还需要如下插件的配合:

    org.springframework.boot     spring-boot-maven-plugin     ${spring-boot.version}             true                                     org.projectlombok                 lombok                                                 repackage                             repackage                        

使用 maven 执行 clean and package ,将在 target\classes 下生成 git.json 文件,内容如下:

#Generated by Git-Commit-Id-Plugin git.build.time=2024-02-21T10\:41\:24+0800 git.build.version=0.0.1-SNAPSHOT git.commit.id.abbrev=3fc9c80 git.commit.id.full=3fc9c8009a48e22ef171c98a97398005e9f30a4a

同时,如果我们反编译生成的 jar 包,将在 BOOT-INF/classes 下看到 git.json 文件

GitCommitIdMavenPlugin 插件有丰富的配置选项,更多配置参考:

        ${project.basedir}/.git         git             yyyy-MM-dd'T'HH:mm:ssZ             ${user.timezone}         false         true         ${project.build.outputDirectory}/git.properties         properties         true             false             true             true             false             false             false                                                                                     false             30000             7             flat                             false                 true                 7                 -dirty                 *                 false                 false                                                 validating project version                         ${project.version}                                                         true             HEAD                    useBranchNameFromBuildEnvironment>true true 查询 Git CommitInfo

通过编写一个接口,用来查询生成的 GitCommitInfo,核心代码如下:

import lombok.extern.slf4j.Slf4j; import org.springframework.util.ResourceUtils; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.io.*; @Slf4j @RestController @RequestMapping("/version") public class VersionController {     /**      * 获取 git.json 中的内容      * @return      * @throws IOException      */     @GetMapping("/gitCommitId")     public String getGitCommitId() throws IOException {         //git.json  or  git.properties         File file = ResourceUtils.getFile("classpath:git.json");         if (file.exists()) {             String s = "";             InputStreamReader in = new InputStreamReader(new FileInputStream(file), "UTF-8");             BufferedReader br = new BufferedReader(in);             StringBuffer content = new StringBuffer();             while ((s = br.readLine()) != null) {                 content = content.append(s);             }             return content.toString();         } else {             return "";         }     } } 兼容性 与 Java 的兼容性 java8:插件版本 = 4.x java11:插件版本 >= 5 与 Maven 的兼容性 maven3:插件版本 = 4.x maven3.2.x:插件版本 >= 7 引用 https://search.maven.org/artifact/io.github.git-commit-id/git-commit-id-maven-plugin https://github.com/git-commit-id/git-commit-id-maven-plugin


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3